package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.MyCrudService;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.sys.dao.TbOfflineShopingOrderLogDao;
import com.thinkgem.jeesite.modules.sys.dao.TbShopProductDao;
import com.thinkgem.jeesite.modules.sys.entity.TbOfflineShoppingOrderInfo;
import com.thinkgem.jeesite.modules.sys.entity.TbOfflineShoppingOrderLog;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Formatter;
import java.util.List;

/**
 * Created by Z15-63 on 2016/12/5.
 */
@Service
public class TbOfflineShoppingOrderLogServece extends MyCrudService<TbOfflineShopingOrderLogDao, TbOfflineShoppingOrderLog> {
    @Autowired
    private TbOfflineShopingOrderLogDao tbOfflineShopingOrderLogDao;
    @Transactional(readOnly = true)
    public Page<TbOfflineShoppingOrderLog> selectOfflineShoppingLog(Page<TbOfflineShoppingOrderLog> page, TbOfflineShoppingOrderLog orderLog) {
        User user = UserUtils.getUser();
        orderLog.setUserId(user.getId());
//        orderLog.setUserId("c74ad407565444dba660c3a4d6071fae");//
        // 设置默认时间范围，默认当前月
      /*  if (orderLog.getStartTime() == null) {
            orderLog.setStartTime(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
        }
        if (orderLog.getEndTime() == null) {
            orderLog.setEndTime(DateUtils.addMonths(orderLog.getStartTime(), 1));
        }*/
        Date date=orderLog.getEndTime();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
        return super.findPage(page, orderLog);
    }
    //得到各支付方式所得到的的金额
    @Transactional(readOnly = true)
    public List findMoney(TbOfflineShoppingOrderLog orderLog){
        List money=new ArrayList();
        User user = UserUtils.getUser();
        orderLog.setUserId(user.getId());
        //orderLog.setUserId("c74ad407565444dba660c3a4d6071fae");
        Date date=orderLog.getEndTime();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
        List<TbOfflineShoppingOrderLog> list=tbOfflineShopingOrderLogDao.findListMoney(orderLog);
        //1 支付宝 2微信 3现金 4物业钱包 5麻雀钱包 0 其他
        BigDecimal zhifubao=new BigDecimal("0");
        BigDecimal weixin=new BigDecimal("0");
        BigDecimal xianjin=new BigDecimal("0");
        BigDecimal wuye=new BigDecimal("0");
        BigDecimal maqu=new BigDecimal("0");
        BigDecimal other=new BigDecimal("0");
        for(int i=0;i<list.size();i++){
            TbOfflineShoppingOrderLog ts=list.get(i);
            if(ts.getPayType()==1){
                zhifubao=ts.getGiveMoney();
            }
            if(ts.getPayType()==2){
                weixin=ts.getGiveMoney();
            }
            if(ts.getPayType()==3){
                xianjin=ts.getGiveMoney();
            }
            if(ts.getPayType()==4){
                wuye=ts.getGiveMoney();
            }
            if(ts.getPayType()==5){
                maqu=ts.getGiveMoney();
            }
            if(ts.getPayType()==0){
                other=ts.getGiveMoney();
            }

        }
        BigDecimal max=new BigDecimal("0");
        money.add(zhifubao);
        money.add(weixin);
        money.add(xianjin);
        money.add(wuye);
        money.add(maqu);
        money.add(other);
        for(int i=0;i<money.size();i++){
            if(max.compareTo((BigDecimal)money.get(i))<=0){
                max=(BigDecimal) money.get(i);
            }
        }
        money.add(max);
        return money;
    }
    @Transactional(readOnly = true)
    public List<TbOfflineShoppingOrderInfo> selectoffineShoppingOrderInfo(Integer orderId){
        List<TbOfflineShoppingOrderInfo> info = tbOfflineShopingOrderLogDao.selectoffineShoppingOrderInfo(orderId);
        return info;
    }

}
